<?php
$page_title = 'eTAT: Home';
require_once 'controller.php';
if(isset($_POST['add_course_submit'])) {
	$data = (object) array('title'=>$_POST['title'],'term'=>"{$_POST['term']} {$_POST['year']}",'description'=>$_POST['description']);
	$id = $instructor->addCourse($data);
	redirect("home.php#course-$id");
}
if(isset($_POST['modify_course_submit'])) {
	$c = $instructor->getCourse($_POST['id']);
	$c->title = $_POST['title'];
	$c->term = $_POST['term'].' '.$_POST['year'];
	$c->description = $_POST['description'];
	$c->updateCourse();
	redirect("home.php#course-$c->id");
}
if(isset($_POST['delete_course_submit'])) {
	$instructor->removeCourse($_POST['id']);
	redirect("home.php");
}
if(isset($_POST['add_group_submit'])) {
	$c = $instructor->getCourse($_POST['course_id']);
	$data->name = $_POST['name'];
	$data->description = $_POST['description'];
	$data->code_name = $_POST['code_name'];
	$data->groups_name = $_POST['groups_name'];
	$c->addGroup($data);
	redirect("home.php#course-$c->id");
}
if(isset($_POST['modify_group_submit'])) {
	$g = new Group($_POST['group_id']);
	$g->name = $_POST['name'];
	$g->description = $_POST['description'];
	$g->code_name = $_POST['code_name'];
	$g->groups_name = $_POST['groups_name'];
	$g->updateGroup();
	redirect("home.php#course-$g->course_id#group-$g->id");
}
if(isset($_POST['delete_group_submit'])) {
	$g = new Group($_POST['group_id']);
	$course_id = $g->course_id;
	$g->remove();
	redirect("home.php#course-$course_id");
}
if(isset($_POST['add_student_submit'])) {
	$group = new Group($_POST['group_id']);
	$data->name = $_POST['full_name'];
	$data->email = $_POST['email'];
	$group->addStudent($data);
	redirect("home.php#course-$group->course_id#group-$group->id");
}
if(isset($_POST['modify_student_submit'])) {
	$s = new Student($_POST['student_id']);
	$group = new Group($s->group_id);
	$s->full_name = $_POST['full_name'];
	$s->email = $_POST['email'];
	$s->updateStudent();
	redirect("home.php#course-$group->course_id#group-$group->id");
}
if(isset($_POST['delete_student_submit'])) {
	$s = new Student($_POST['student_id']);
	$group = new Group($s->group_id);
	$s->remove();
	redirect("home.php#course-$group->course_id");
}
if(isset($_POST['annotate_course_submit'])) {
	$course = new Course($_POST['course_id']);
	$instructor->addCourseAnnotation($course->id,$_POST['annotation']);
	redirect("home.php#course-$course->id");
}
require_once 'header.php';
?>
<h1>Home</h1>
<h3>Courses:</h3>

<?php
$courses = $instructor->getAllCourses();
if($courses != null) {
	foreach($courses as $c) {
?>
<div id='course-<?php print $c->id; ?>' class='course'>
	<a href='javascript: expandCourse("course-<?php print $c->id; ?>")' class='expand'><?php print $c->title; ?></a>
	<div class='course-options'>
		<a href='javascript: popup("modifycourse.php?c=<?php print $c->id; ?>")'>modify</a> | 
		<a href='javascript: popup("deletecourse.php?c=<?php print $c->id; ?>")'>delete</a> | 
		<?php
			$annotations = count($c->getAnnotations());
			if($annotations > 0) {
				$annotation_href = $annotations > 1? "$annotations annotations" : "1 annotation";	
			} else {
				$annotation_href = 'annotate';
			}
		?>
		<a href='javascript: popup("annotatecourse.php?c=<?php print $c->id; ?>")'><?php print $annotation_href; ?></a>
	</div>
</div>
<?php
	}
} else {
?>
<p>Looks like you haven't added any courses for monitoring, yet.</p>
<p>Let's get started by pressing the <b>Add Course</b> button below:</p>
<?php
}
print '<br />';
print button('add_course','Add Course',array('type'=>'button','onclick'=>'popup("addcourse.php")'));
require_once 'footer.php';
?>